var express = require('express');
var router = express.Router();

//加载模型
const CateModel = require('../../models')['Cate']
const NewsModel = require('../../models')['New']

//获取图片新闻
router.get('/listImg/:cate_id',async (req,res)=>{
    try{
        //接收分类id
        let cate_id = req.params.cate_id;
        //根据分类id获取图片新闻
        const image_news = await NewsModel.findAll({
            attributes: ['id', 'title', 'img_url'],
            where: {
                'cateId': cate_id
            },
            order: [
                ['view_nums', 'DESC']
            ],
            limit: 5
        })
        res.cc(200, '请求成功',image_news)
    }catch(e){
        res.cc(400,e.message)
    }
     
})
//获取分类新闻
router.get('/listNews/:cate_id',async (req, res) => {
    try{
        //接收分类id
        let cate_id = req.params.cate_id;
        //根据分类id获取新闻
        const new_news = await NewsModel.findAll({
            attributes: ['id', 'title', 'createdAt','Cate.cate_name'],
            where: {
                'cateId': cate_id
            },
            order: [
                ['createdAt', 'DESC']
            ],
            include:[CateModel],
            limit: 10
        })
        res.cc(200, '请求成功',new_news)
    }catch(e){
        res.cc(400,e.message)
    }
    
})

module.exports=router